//
//  NSString+Replacement.m
//  DollsVending
//
//  Created by Don on 2017/9/12.
//
//

#import "NSString+Replacement.h"

@implementation NSString (Replacement)

- (NSString *)replaceStrings:(NSArray *)strings withString:(NSString *)string {
    NSCAssert(strings != nil,@"NSString Method: (replaceStrings:withString:)  Invalid parameter not satisfying: strings");
    NSCAssert(string != nil,@"NSString Method: (replaceStrings:withString:)  Invalid parameter not satisfying: string");
    
    NSString *newStr = self;
    if (!NSStringIsNullOrEmpty(newStr)) {
        for (NSString *miniString in strings) {
            NSRange strRange = [self rangeOfString:miniString];
            if (strRange.location != NSNotFound) {
                newStr = [newStr stringByReplacingOccurrencesOfString:miniString withString:string];
            }else {
                continue;
            }
            
        }
    }else {
        newStr = @"";
    }
    
    return newStr;
}

- (NSString *)replaceTheFirstOneString:(NSString *)str withString:(NSString *)str2 {
    NSCAssert(str != nil,@"NSString Method: (replaceTheFirstOneString:)  Invalid parameter not satisfying: str");
    NSCAssert(str2 != nil,@"NSString Method: (replaceTheFirstOneString:)  Invalid parameter not satisfying: str2");
    
    
    NSString *newStr = self;
    if (!NSStringIsNullOrEmpty(str)) {
        NSRange strRange = [self rangeOfString:str];
        if (strRange.location != NSNotFound) {
            newStr = [newStr stringByReplacingCharactersInRange:strRange withString:str2];
        }
    }
    
    return newStr;
}

- (BOOL)isContainString:(NSString *)string {
    if (!NSStringIsNullOrEmpty(string)) {
        NSRange strRange = [self rangeOfString:string];
        if (strRange.location != NSNotFound) {
            return YES;
        }
    }
    
    return NO;
}
@end
